<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>当前时间+停留时间</title>
</head>
<body>
    <p id = 'nowTime'></p>
    <p id = 'stayTime'>持续时间：0时0分0秒</p>

    <script>
        // 封装时间格式化
        function time(){
            // 创建当前时间
            var date = new Date();

            // 年月日时分秒星期
            var year = date.getFullYear();

            // 个位数补0
            var month = date.getMonth()+1;
            month = month<10?'0'+month:month;

            var day = date.getDate();
            day = day<10?'0'+day:day;

            var hour = date.getHours();
            hour = hour<10?'0'+hour:hour;

            var minute = date.getMinutes();
            minute = minute<10?'0'+minute:minute;

            var second = date.getSeconds();
            second = second<10?'0'+second:second;

            var week = date.getDay();

            // 根据星期索引确定星期几
            var weekArr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六',]

            return year+'-'+month+'-'+day+' '+ hour+':'+minute+':'+second+ ' ' +weekArr[week];
        }

        // 记录登录时间
        var logintime = new Date();
        console.log()

        // 初始化
        nowTime.innerHTML = '当前时间：' + time();

        setInterval(function(){

            // 记录当前时间
            var nowtime = new Date();
            // 时差
            var jetLag = nowtime - logintime;
            // 转化时分秒
            var hour = parseInt(jetLag/1000/60/60);
            var minute = parseInt((jetLag/1000/60)%60);
            var second = parseInt((jetLag/1000)%60);

            // 渲染
            nowTime.innerHTML = '当前时间：' + time();
            stayTime.innerHTML = '持续时间：' + hour +'时'+minute+'分'+second+'秒';
        },1000)

        
    </script>
</body>
</html>